import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

import 'PicturePage.dart';
import 'video_tab_page.dart';

class AssetVideoPage extends StatefulWidget {
  final String videoAsset;

  const AssetVideoPage(this.videoAsset, {super.key});
  @override
  AssetVideoPageState createState() => AssetVideoPageState(videoAsset);
}

class AssetVideoPageState extends State<AssetVideoPage>
    with WidgetsBindingObserver {
  // List<String> videos = ['assets/trailer.mp4', 'assets/register.mp4'];
  late VideoPlayerController _controller;

  String videoAsset;
  AssetVideoPageState(this.videoAsset);

  @override
  void initState() {
    super.initState();
    setAndPlayVideo();
  }

  setAndPlayVideo() async {
    _controller = VideoPlayerController.asset(videoAsset);
    _controller.addListener(() {
      setState(() {});
    });
    await _controller.initialize();
    await _controller.play();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(videoAsset)),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Container(
              padding: const EdgeInsets.all(20),
              child: Center(
                child: AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: VideoPlayer(_controller),
                ),
              ),
            ),
          ),
          ElevatedButton(
            onPressed: () {
              Navigator.push(context, MaterialPageRoute(
                builder: (BuildContext context) {
                  return const PicturePage();
                },
              ));
            },
            child: const Text("界面跳转"),
          ),
          Container(
            padding: const EdgeInsets.all(20),
          )
        ],
      ),
    );
  }
}
